using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using FarPoint.Win;
using FarPoint.Win.Spread;

namespace Spread.Sparkline.Features
{
    public partial class ScatterSparkline : Spread.Common.Features.Sample.BaseSampleControl
    {
        public ScatterSparkline()
        {
            InitializeComponent();
            Name = "Scatter";

            List<Color> colors = new List<Color>
            {
                Color.White,
                Color.Black,
                Color.Orange,
                Color.LightGray,
                Color.Gray,
                Color.DarkGray,
                Color.Green
            };

            comboColor1.DataSource = colors;
            comboColor1.SelectedItem = Color.Black;

            comboColor2.DataSource = colors.ToList();
            comboColor2.SelectedItem = Color.Green;

            InitSpreadsheet(fpSpread1.ActiveSheet);

            comboColor1.SelectedIndexChanged += ComboColor_SelectedIndexChanged;
            comboColor2.SelectedIndexChanged += ComboColor_SelectedIndexChanged;
        }

        #region Private methods
        private void InitSpreadsheet(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Cells["A1:E1"].Merge();
            worksheet.Cells["A1"].Value = "Rainfall effect on Particulate Levels and Temperature";
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 13 });
            sheetView.Cells["A1"].BackColor = ColorTranslator.FromHtml("#BBBBBB");
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;

            worksheet.SetValue(1, 0, new object[,]
            {
                { "Daily Rainfall", "Particulate Level", "Daily Rainfall1", "Temperature", "Diagram" },
                { 2.0, 100, 2.0, 15, null },
                { 2.5, 130, 2.5, 12, null },
                { 3.0, 120, 3.0, 11, null },
                { 3.5, 140, 3.5, 9, null },
                { 4.0, 120, 4.0, 10, null },
                { 4.5, 110, 4.5, 10, null },
                { 5.0, 110, 5.0, 9, null },
                { 5.5, 105, 5.5, 9, null },
                { 6.0, 100, 6.0, 8, null }
            });
            worksheet.Cells["E3:E11"].Merge();

            worksheet.Cells["E3"].Formula2 = "SCATTERSPARKLINE(A3:B11,C3:D11,,,,,AVERAGE(B3:B11),AVERAGE(A3:A11),,,,,"
                + $"TRUE,TRUE,TRUE,\"{((Color)comboColor1.SelectedItem).Name}\",\"{((Color)comboColor2.SelectedItem).Name}\",TRUE)";

            worksheet.Rows["1:11"].RowHeight = 25;

            worksheet.Cells["A2:E2"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 11, Bold = true });
            worksheet.Cells["A2:E2"].Borders.ApplyBorder(new GrapeCity.Spreadsheet.Border()
            {
                Bottom = new GrapeCity.Spreadsheet.BorderLine(GrapeCity.Spreadsheet.BorderLineStyle.Thin, GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black))
            });

            worksheet.Cells["A3:D11"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.Left;
            worksheet.Columns["A"].ColumnWidth = 130;
            worksheet.Columns["B"].ColumnWidth = 130;
            worksheet.Columns["D"].ColumnWidth = 130;
            worksheet.Columns["E"].ColumnWidth = 200;

            // Hide 2nd rainfall column data
            worksheet.Columns["C"].Hidden = true;
        }
        #endregion

        #region Event handlers
        private void ComboColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            fpSpread1.Sheets[0].AsWorksheet().Cells["E3"].Formula2 = "SCATTERSPARKLINE(A3:B11,C3:D11,,,,,AVERAGE(B3:B11),AVERAGE(A3:A11),,,,,"
                + $"TRUE,TRUE,TRUE,\"{((Color)comboColor1.SelectedItem).Name}\",\"{((Color)comboColor2.SelectedItem).Name}\",TRUE)";
        }
        #endregion
    }
}


//
//ScatterSparkline.Designer.cs
//



namespace Spread.Sparkline.Features
{
    partial class ScatterSparkline
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ScatterSparkline));
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("pnlMain.Controls"))));
            this.fpSpread1_Sheet1 = this.fpSpread1.GetSheet(0);
            this.label1 = new System.Windows.Forms.Label();
            this.comboColor1 = new System.Windows.Forms.ComboBox();
            this.comboColor2 = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.comboColor2);
            this.pnlControls.Controls.Add(this.label2);
            this.pnlControls.Controls.Add(this.comboColor1);
            this.pnlControls.Controls.Add(this.label1);
            this.pnlControls.Size = new System.Drawing.Size(210, 599);
            // 
            // fpSpread1
            // 
            this.fpSpread1.AccessibleDescription = "Book1, Sheet1, Row 0, Column 0";
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Font = new System.Drawing.Font("Calibri", 11F);
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Size = new System.Drawing.Size(636, 599);
            this.fpSpread1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(4, 31);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(254, 17);
            this.label1.TabIndex = 0;
            this.label1.Text = "Rainfall - Particulate Level Graph Color";
            // 
            // comboColor1
            // 
            this.comboColor1.FormattingEnabled = true;
            this.comboColor1.Location = new System.Drawing.Point(7, 60);
            this.comboColor1.Name = "comboColor1";
            this.comboColor1.Size = new System.Drawing.Size(121, 24);
            this.comboColor1.TabIndex = 1;
            // 
            // comboColor2
            // 
            this.comboColor2.FormattingEnabled = true;
            this.comboColor2.Location = new System.Drawing.Point(9, 130);
            this.comboColor2.Name = "comboColor2";
            this.comboColor2.Size = new System.Drawing.Size(121, 24);
            this.comboColor2.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(6, 101);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(231, 17);
            this.label2.TabIndex = 2;
            this.label2.Text = "Rainfall - Temperature Graph Color";
            // 
            // ScatterSparkline
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "ScatterSparkline";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            this.pnlControls.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
        private System.Windows.Forms.ComboBox comboColor2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboColor1;
        private System.Windows.Forms.Label label1;
    }
}
